﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;
using System.Text;
using System.Data;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;
using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;
using CardsServer.Tools;
namespace CardsServer.WCF
{
    public partial interface IService
    {
        [OperationContract]
        string cardin_Add(cardin cardin, int ACID);

        [OperationContract]
        List<string> numervaild(List<string> numbers,int ACID);

        [OperationContract]
        string Bcardin_Add(Bcardin cardin, int ACID);

        [OperationContract]
        List<View_Goods_Lite> View_BlankCard(int ACID);
    }
    public partial class Service : IService
    {

        /// <summary>
        /// 套餐购入
        /// </summary>
        /// <param name="cardin"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        public string cardin_Add(cardin cardin, int ACID)
        {
            string r = "";
            using (TransactionScope tc = new TransactionScope())
            {
                try
                {

                    using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                    {
                       string r1 = CardIN_M_Add(cardin.M, ACID, ctx);
                       if (!string.IsNullOrEmpty(r1))
                       {
                           throw new Exception(r1);
                       }
                        foreach (CardIN_D item in cardin.D)
                        {
                           string r2 = CardIN_D_Add(item, ACID, ctx);
                           if (!string.IsNullOrEmpty(r2))
                           {
                               throw new Exception(r2);
                           }

                        }
                        foreach (numberpool n in cardin.numbers)
                        {
                            string r3 = numberpool_Add(n, ACID, ctx);
                            if (!string.IsNullOrEmpty(r3))
                            {
                                throw new Exception(r3);
                            }
                        }
                        RecordBalance(cardin.M.total,cardin.M.ClientID.Value,ctx, ACID);
                        ctx.SubmitChanges();
                        tc.Complete();
                    }
                }
                catch (Exception ex)
                {
                    r = ex.Message;
                }
            }
            return r;
        }

        /// <summary>
        /// 空卡入库
        /// </summary>
        /// <param name="cardin"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        public string Bcardin_Add(Bcardin cardin, int ACID)
        {
            string r = "";
            using (TransactionScope tc = new TransactionScope())
            {
                try
                {

                    using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                    {
                        string r1 = BlankCardIN_M_Add(cardin.M, ACID, ctx);
                        if (!string.IsNullOrEmpty(r1))
                        {
                            throw new Exception(r1);
                        }
                        foreach (BlankCardIN_D item in cardin.D)
                        {
                            string r2 = BlankCardIN_D_Add(item, ACID, ctx);
                            if (!string.IsNullOrEmpty(r2))
                            {
                                throw new Exception(r2);
                            }

                        }
                        foreach (Cards card in cardin.cards)
                        {
                            string r4 = Cards_Add(card, ACID, ctx);
                            if (!string.IsNullOrEmpty(r4))
                            {
                                throw new Exception(r4);
                            }
                        }
                        foreach (var kc in cardin.D.GroupBy(p => p.GoodCode))
                        {
                            onhand o = null;
                            if (ctx.onhand.Count(p => p.GoodsCode == kc.Key && p.WarehouseID == cardin.M.WarehouseID)==0)
                            {
                                o = new onhand();
                                o.GoodsCode = kc.Key;
                                o.onhandcount += kc.Count();
                                o.WarehouseID = cardin.M.WarehouseID;
                                onhand_Add(o, ACID, ctx);
                            }
                            else
                            {
                                o = ctx.onhand.Single(p => p.GoodsCode == kc.Key && p.WarehouseID == cardin.M.WarehouseID);
                                o.onhandcount += kc.Count();
                            }
                        }
                        RecordBalance(cardin.M.total, cardin.M.ClientID.Value, ctx, ACID);
                        ctx.SubmitChanges();
                        tc.Complete();
                    }
                }
                catch (Exception ex)
                {
                    r = ex.Message;
                }
            }
            return r;
        }

        public List<string> numervaild(List<string> numbers, int ACID)
        {
            List<string> r = new List<string>();
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                foreach (string item in numbers)
                {
                    if (numberpool_GetCount(p => p.phone == item, ACID) > 0)
                    {
                        r.Add(item);
                    }
                }
            }
            return r;
        }
        
        public List<View_Goods_Lite> View_BlankCard(int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                return ctx.View_Goods_Lite.Where(p => p.isBlankCard).ToList();
            }
        }
    }

    


    /// <summary>
    /// 套餐入库单
    /// </summary>
    public class cardin
    {
        public CardIN_M M { get; set; }
        public List<CardIN_D> D { get; set; }
        public List<numberpool> numbers { get; set; }
    }

    /// <summary>
    /// 空卡入库单
    /// </summary>
    public class Bcardin
    {
        public BlankCardIN_M M { get; set; }
        public List<BlankCardIN_D> D { get; set; }
        public List<Cards> cards { get; set; }
    }

}

